# V figure 60 days: Intensity - share of death in X days after statr admission: ------------------------------


build_adm_month <- function(dt_hosp_raw_1_row=  do_table_A2(top_All_prof_10=top_All_prof_10) , 
                       dt_dyn  = build_dyn_new() ) {
  # need hosp data from table a2 
  dt_hosp_cost_cancer_dyn_prob<- merge(x = dt_hosp_raw_1_row[, pos := .I],
                                       y = dt_dyn[,
                                                  .(id_var ,
                                                    DMG_date_of_death_XX,
                                                    dyn_index_date_XX = S_index_date_XX,
                                                    month_from_index,
                                                    dyn_prob = prob_for_report)],
                                       by.x ="id_var",
                                       by.y = "id_var",
                                       all.data=TRUE,
                                       allow.cartesian=TRUE)
  
  
  dt_hosp_cost_cancer_dyn_prob[,diff_index := 
                                 as.numeric(difftime(
                                   dyn_index_date_XX ,
                                   min_event_date,
                                   units = "days"))  ]
  
  #  keep only before , find the minumun difference in days of "before", keep only the close to the event:
  
  dt_hosp_cost_cancer_dyn_prob <- 
    dt_hosp_cost_cancer_dyn_prob[diff_index<=0,][
      ,min_diff := max(diff_index), by=pos][
        min_diff==diff_index][
          ,c("min_diff"):=NULL]
  
  return(dt_hosp_cost_cancer_dyn_prob)
}

do_figure60days <- function(dt_hosp_cost_cancer_dyn_prob ) { 
  dt_hosp_cost_cancer_dyn_prob[,date_start_death := as.numeric(difftime(
    DMG_date_of_death_XX, min_event_date, units = "days"))]
  dt_hosp_cost_cancer_dyn_prob[, `:=`(died_7_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=7) ,
                                      died_14_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=14) ,
                                      died_30_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=30) ,
                                      died_60_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=60) ) 
                               ]
  
  
  dt_hosp_cost_cancer_dyn_prob[,Intensity := ifelse(new_profession%in%intensity_wards,
                                                    "Low","High")]
  
  
  death_adm_dyn_bins_Intensity <- dt_hosp_cost_cancer_dyn_prob[,
                                                               .("7 days"= mean(died_7_days),
                                                                 "14 days"= mean(died_14_days),
                                                                 "30 days"= mean(died_30_days),
                                                                 "60 days"= mean(died_60_days),
                                                                 N=as.double(.N)),
                                                               
                                                               by= .(Intensity , 
                                                                     bins = plyr::round_any(dyn_prob, 0.10, floor)) ][
                                                                       order(bins)]
  
  
  combine_death_adm_bins_Intensity <- rbind(
    death_adm_dyn_bins_Intensity[,type:="Near the Event"] 
  )
  
  combine_death_adm_bins_long_Intensity <- 
    melt(combine_death_adm_bins_Intensity,
         id.vars= c("bins","type","Intensity") ,
         measure.vars = names(death_adm_dyn_bins_Intensity)[3:6])
  
  combine_death_adm_bins_long_Intensity[,variable:=factor(variable, levels = c("60 days", "30 days", "14 days" ,"7 days"))]
  
  
  write.csv(combine_death_adm_bins_long_Intensity[variable=="60 days" &
                                                    type == "Near the Event",],
            file="Death_within_60_days_after_adm_Intensity_dynamic_1row.csv")
  
  
  pdf(file="Death_within_60_days_after_adm_Intensity_dynamic_1row.pdf")
  print(ggplot()+
          geom_density(data = dt_hosp_cost_cancer_dyn_prob,
                       aes(x=dyn_prob, y=..scaled.., fill=Intensity),
                       size = 0.7,
                       color=NA,
                       alpha = 0.2) +
          geom_line(data =combine_death_adm_bins_long_Intensity[variable=="60 days" &
                                                                  type == "Near the Event",],
                    aes(x=(bins+0.05) ,y= value, linetype = Intensity))+
          scale_x_continuous(breaks = seq(0, 1, 0.10), limits = c(0,1)) +
          scale_y_continuous(breaks = seq(0, 1, 0.10), limits = c(0,1)) +
          scale_fill_manual( values = c("grey5","grey50"))+
          labs(y="",
               x = "Current Predicted Mortality Risk",
               linetype="Intensity",
               fill ="Intensity")+
          ylim(0,1) +
          theme(legend.position = "none") +
          theme(aspect.ratio = 1)   +
          annotate(geom="text", x=0.7, y=0.07, label="Low Intensity", size = 5)+
          annotate(geom="text", x=0.7, y=0.02, label="High Intensity", size = 5)+
          annotate(geom="text", x=0.73, y=0.6, label="Low Intensity", size = 5)+
          annotate(geom="text", x=0.94, y=0.42, label="High Intensity", size =5 )
  )
  dev.off()
  
}
